var raceInfoStr = '	<div id="raceInfo" class="raceInfo">'
					+'<div id="raceDateArea"><label>開催：</label><input type="text" id="raceDate" name="raceDate" value="" /></div>'
					+'<div id="raceNameArea"><label>レース名：</label><input type="text" id="raceName" name="raceName" value="" /></div>'
					+'<div id="raceInfo1Area"><label>レース情報１：</label><input type="text" id="raceInfo1" name="raceInfo1" value="" /></div>'
					+'<div id="raceInfo2Area"><label>レース情報２：</label><input type="text" id="raceInfo2" name="raceInfo2" value="" /></div>'
					+'<div id="raceInfo3Area"><label>レース情報３：</label><input type="text" id="raceInfo3" name="raceInfo3" value="" /></div>'
					+'<div id="startTimeArea"><label>開始時間：</label><input type="text" id="startTime" name="startTime" value="" /></div>'
					+'<div id="deadlineArea"><label>終了時間：</label><input type="text" id="deadline" name="deadline" value="" /></div>'
					+'<div id="districtClassArea"><label>地域区分：</label><select id="districtClass" name="districtClass"><option value="1">中央</option><option value="2">地方</option></select></div>'
					+'<div id="progressArea"><label>進捗：</label><input type="text" id="progress" name="progress" value="" /></div>'
					+'<div id="currentFlgArea"><label>現在フラグ：</label><input type="text" id="currentFlg" name="currentFlg" value="" /></div>'
					+'<input type="hidden" id="kaisaiNo" name="kaisaiNo" value="" />'
					+'<input type="hidden" id="raceNo" name="raceNo" value="" />'
					+'<input type="hidden" id="raceDate" name="raceDate" value="" />'
					+'<input type="hidden" id="key" name="key" value="" />'
					+'</div>';


var nomiHorseStr = '<div id="nomiHorseRowArea" class="nomiHorseL">'
					+'馬名：<input type="text" class="horseNm" name="horseName" value="" />'
					+'斤量：<input type="text" class="num kinryo" name="weight" value=""/>'
					+'</div>';

var rcHorseStr = '<div id="rcHorseRowArea" class="rcHorseL">'
					+'馬番：<input type="text" class="num horseNo" name="horseNo" value="" />'
					+'馬名：<input type="text" class="horseNm" name="horseName" value="" />'
					+'性齢：<input type="text" class="sexAge" name="sexAge" value=""/>'
					+'斤量：<input type="text" class="num kinryo" name="weight" value=""/>'
					+'騎手：<input type="text" name="jockey" value=""/>'
					+'取消：<input type="checkbox" class="" name="exclusionFlg" value="true" />'
					+'</div>';

var rsHorseStr = '<div id="rsHorseRowArea" class="rsHorseL">'
					+'着順：<input type="text" class="num horseNo" name="finishPoint" value="" />'
					+'馬番：<input type="text" class="num horseNo" name="horseNo" value="" />'
					+'馬名：<input type="text" class="horseNm" name="horseName" value="" />'
					+'性齢：<input type="text" class="sexAge" name="sexAge" value=""/>'
					+'斤量：<input type="text" class="num kinryo" name="weight" value=""/>'
					+'騎手：<input type="text" name="jockey" value=""/>'
					+'</div>';

var lineStr = {
		raceInfo:raceInfoStr,
		nomiHorseL:nomiHorseStr,
		rcHorseL:rcHorseStr,
		rsHorseL:rsHorseStr
}

$(function(){
	hideArea();

	$("#raceInfoArea").append(raceInfoStr);

	//特別登録取得クリックイベント
	$("#btnNomination").bind("click",btnNomination_click);

	//ノミネーション登録クリックイベント
	$("#btnNomiReg").bind("click",btnNomiReg_click);

	//特別登録馬1行追加クリックイベント
	$("#btnNomiRowAdd").click(function(e){
		$("#nomiHorseListArea").append(nomiHorseStr);
	});

	//特別登録馬1行削除クリックイベント
	$("#btnNomiRowDel").click(function(e){
		$(".nomiHorseL:last").remove();
	});

	//特別登録馬全行削除クリックイベント
	$("#btnNomiRowAllDel").click(function(e){
		$(".nomiHorseL").remove();
	});

	//出走馬検索クリックイベント
	$("#btnRaceCard").bind("click",btnRaceCard_click);

	//出走馬1行追加クリックイベント
	$("#btnRcRowAdd").click(function(e){
		$("#rcHorseListArea").append(rcHorseStr);
	});

	//出走馬1行削除クリックイベント
	$("#btnRcRowDel").click(function(e){
		$(".rcHorseL:last").remove();
	});

	//出走馬全行削除クリックイベント
	$("#btnRcRowAllDel").click(function(e){
		$(".rcHorseL").remove();
	});

	//出走馬検索クリックイベント
	$("#btnRaceCard").bind("click",btnRaceCard_click);

	//出走馬登録クリックイベント
	$("#btnRcReg").bind("click",btnRcReg_click);

	//競争結果検索クリックイベント
	$("#btnResult").bind("click",btnResult_click);

	//競争結果登録クリックイベント
	$("#btnRsReg").bind("click",btnRsReg_click);

	//レース削除
	$("#btnRaceDel").bind("click",btnRaceDel_click);

	$(":button").click(function(e){
		setProp();
	});

	$(".num").live("keyup",function(e){

	});

});

function setProp(){
	$(".kinryo").attr("maxlength","4");
	$(".kinryo").mask("99.9",{placeholder:" "});
	$(".horseNo").attr("maxlength","2");
	$("#statusArea").html("");
	//$(".horseNo").mask("99",{placeholder:""});
	//$.mask.definitions['ア']='[アイウエオカキクケコサシスセソタチステトナニヌネノハヒフヘホマミムメモヤゐユゑヨワヲンー]';
	//$.mask.definitions['ア']='[ア-ンー]';
	//$(".horseNm").mask("ア????????????????");
	$("#resultArea :not(input[name=finishPoint])").attr({
		readOnly:	"true"
	});
};

function setRaceInfo(json){
	setResponseData(json,lineStr);
}

function hideArea(){
	$("#raceInfoArea").hide();
	$("#nominationArea").hide();
	$("#raceCardArea").hide();
	$("#resultArea").hide();
};

function btnNomination_click(e){

	hideArea();
	if($("input[name=raceKey]:checked").val() == "new"){
		$("#raceInfoArea input[type=text]").val("");
		$("#nomiHorseListArea > div").remove();
		$("#raceInfoArea").show();
		$("#nominationArea").show();
		return;
	}

	query = {};
	query["raceURL"] = $("#raceURL").val();
	query["raceKey"] = $("input[name=raceKey]:checked").val();
	query[REQKEY_COMMAND] = "search";
	query[REQKEY_PROC] = proc;

	$.ajax({
		type: "POST",
		url: nomiURL,
		data: query,
		success: function(json){
			$("#nomiHorseListArea > div").remove();
			setRaceInfo(json);

			$(statusArea).html("");
			$("#raceInfoArea").show();
			$("#nominationArea").show();
			setProp();
		},
		error: function(request){
			$(statusArea).html("取得失敗");
		}
	});
};

function btnNomiReg_click(e){

	query = {};
	query["raceInfo"] = $.toJSON(getInput("#raceInfoArea"));
	query["nomiHorseL"] = $.toJSON(getInputList(".nomiHorseL"));
	query[REQKEY_COMMAND] = "regist";

	$.ajax({
		type: "POST",
		url: nomiURL,
		data: query,
		success: function(request){
			$(statusArea).html("登録完了");
		},
		error: function(request){
			$(statusArea).html("登録失敗");
		}
	});

};

//出走馬検索クリックイベント
function btnRaceCard_click(e){

	hideArea();

	if($("input[name=raceKey]:checked").val() == "new"){
		$("#raceInfoArea input[type=text]").val("");
		$("#rcHorseListArea > div").remove();
		$("#raceInfoArea").show();
		$("#raceCardArea").show();
		return;
	}

	query = {};
	query["raceURL"] = $("#raceURL").val();
	query["raceKey"] = $("input[name=raceKey]:checked").val();
	query[REQKEY_COMMAND] = "search";
	query[REQKEY_PROC] = proc;
	$.ajax({
		type: "POST",
		url: rcURL,
		data: query,
		success: function(json){
			res = json
			$("#rcHorseListArea > div").remove();
			setRaceInfo(json);

			$(statusArea).html("");
			$("#raceInfoArea").show();
			$("#raceCardArea").show();
			setProp();
		},
		error: function(request){
			$(statusArea).html("取得失敗");
		}
	});

};

//出走馬登録クリック
function btnRcReg_click(e){

	query = {};
	query["raceInfo"] =$.toJSON(getInput("#raceInfoArea"));
	query["rcHorseL"] =$.toJSON(getInputList(".rcHorseL"));
	query[REQKEY_COMMAND] = "regist";

	$.ajax({
		type: "POST",
		url: rcURL,
		data: query,
		success: function(request){
			$(statusArea).html("登録完了");
		},
		error: function(request){
			$(statusArea).html("登録失敗");
		}
	});

};

//競争結果検索クリック
function btnResult_click(e){
	hideArea();

	query = {};
	query["raceKey"] = $("input[name=raceKey]:checked").val();
	query[REQKEY_COMMAND] = "search";
	query[REQKEY_PROC] = proc;

	$.ajax({
		type: "POST",
		url: rsURL,
		data: query,
		success: function(json){
			$("#resultArea > #raceDate").val(json.raceInfo.raceDate);

			$("#rsHorseListArea > div").remove();
			setRaceInfo(json);

			$(statusArea).html("");
			$("#resultArea").show();
			setProp();
		},
		error: function(request){
			$(statusArea).html("取得失敗");
		}
	});

};

//競争結果登録クリック
function btnRsReg_click(e){

	query = {};
	query["raceDate"] =$("#resultArea input[name=raceDate]").val();
	query["rsHorseL"] =$.toJSON(getInputList(".rsHorseL"));
	query[REQKEY_COMMAND] = "regist";

	$.ajax({
		type: "POST",
		url: rsURL,
		data: query,
		success: function(request){
			$(statusArea).html("登録完了");
		},
		error: function(request){
			$(statusArea).html("登録失敗");
		}
	});

};

//レース削除
function btnRaceDel_click(e){
	hideArea();

	query = {};
	query["raceKey"] = $("input[name=raceKey]:checked").val();
	query[REQKEY_COMMAND] = "delete";

	$.ajax({
		type: "POST",
		url: riURL,
		data: query,
		success: function(request){
			$("input[name=raceKey]:checked").parent().remove();
			$(statusArea).html("削除完了");
		},
		error: function(request){
			$(statusArea).html("削除失敗");
		}
	});
};
